#include "communication.h"



//服务器接收客户端消息线程处理函数
void communication(int& maxfd, struct pollfd (&fds)[1024], int& cfd)
{
    //处理客户端数据
    char buf[1024] = {0};
    int len = read(fds[cfd].fd, buf, sizeof(buf));
    if(len == -1)
    {
        perror("read");
        return ;
    }
    else if(len == 0)
    {
        std::cout << "客户端断开连接" << std::endl;
        mtx.lock();
        close(cfd);//关闭套接子文件描述符
        fds[cfd].fd = -1;//从集合中删去
        maxfd = maxfd > cfd ? maxfd : cfd;//更新maxfd
        mtx.unlock();
        return ;//退出子线程
    }
    else
    {
        std::cout << "client say: " << buf << std::endl;
        for(int i = 0; i < len; i++)
        {
            buf[i] = std::toupper(buf[i]);
        }
        write(fds[cfd].fd, buf, len);
    }
}
